﻿Imports System.Web.UI
Imports System.Collections.Generic
Imports System.Reflection

Imports DotNetNuke
Imports DotNetNuke.Services.Exceptions
Imports DotNetNuke.Services.Localization

Namespace dnnWerk.Modules.Nuntio.Content

    Partial Class View
        Inherits ContentBase
        Implements Entities.Modules.IActionable

#Region "Private Members"

        Private _content As String = Null.NullString
        Private _title As String = Null.NullString

#End Region

#Region "Event Handlers"

        Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

            GetContent()

            If _title <> Null.NullString Then
                Me.ModuleConfiguration.ModuleTitle = _title
            Else
                Try
                    'try loading module title from previous module versions
                    If Not Settings("PNC_CONTENT_MODULETITLE_" & Convert.ToString(CType(Page, PageBase).PageCulture.LCID)) Is Nothing Then
                        Me.ModuleConfiguration.ModuleTitle = CType(Settings("PNC_CONTENT_MODULETITLE_" & Convert.ToString(CType(Page, PageBase).PageCulture.LCID)), String)
                    End If
                Catch
                End Try
            End If

        End Sub

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try

                'no content found, hide module for non editors
                If _content = Null.NullString AndAlso (Not CanEdit()) Then

                    HideModule()

                Else

                    'send content to parser
                    Dim processor As New ContentProcessor(UserInfo, Server.HtmlDecode(_content), "page", Me.LocalResourceFile, Me.TemplateSourceDirectory, Me.PortalSettings, ModuleId, Me.Settings)
                    processor.Process()

                    'load the content string into a literal object            
                    Dim objLiteral As New Literal
                    objLiteral.Text = processor.ParsedContent

                    plhControls.Controls.Add(objLiteral)

                End If

            Catch exc As Exception        'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

#End Region

#Region "Private Methods"

        Private Sub HideModule()

            'transverse the oControls parent objects until we can locate 
            'the parent container (ascx file) 
            Dim o As Control = Me 'the module object to hide/show 
            Do
                o = o.Parent
                If TypeOf o Is Page Then
                    'never found an ascx parent control 
                    'we're at the page so we're done 
                    Exit Do
                End If
                Try
                    'if the name of the parent object is an ascx file then 
                    'this must be the parent container (we hope!) 
                    If o.ToString.IndexOf("_ascx") <> -1 Then
                        o.Visible = False 'set to hide/show the container of oControl 
                        Exit Do
                    End If
                Catch
                End Try
            Loop

        End Sub

        Private Sub GetContent()

            Dim collContent As New ContentCollection
            Dim ctl As New ContentController
            collContent = ctl.ListContent(ModuleId)

            'let's see how many role content items we have
            Dim lstContent As New List(Of ContentInfo)
            lstContent = collContent.GetRoleContentUnique


            For Each oContent As ContentInfo In lstContent
                'is user authorized for the content?
                If PortalSecurity.IsInRole(oContent.Role) Or oContent.Role.ToLower = "generic" Then

                    'now get the different content objects for that role
                    Dim roleContent As New List(Of ContentInfo)
                    roleContent = collContent.GetContentByRole(oContent.Role)

                    For Each objContent As ContentInfo In roleContent
                        'is content locale the current locale?
                        If objContent.Locale = CurrentLocale Then
                            'is content published?
                            If objContent.IsPublished Then

                                'content found, return  
                                _content = objContent.Content
                                _title = objContent.Title

                                Exit Sub

                            End If
                        End If
                    Next

                    'no content found? Let's look again and see if we can find a content for the fallback locale of the role
                    For Each objContent As ContentInfo In roleContent
                        'is content locale the current locale?
                        If objContent.IsFallback Then
                            'is content published?
                            If objContent.IsPublished Then

                                'content found, return  
                                _content = objContent.Content
                                _title = objContent.Title

                                Exit Sub

                            End If
                        End If
                    Next

                End If
            Next

        End Sub

#End Region

#Region "Optional Interfaces"

        Public ReadOnly Property ModuleActions() As Entities.Modules.Actions.ModuleActionCollection Implements Entities.Modules.IActionable.ModuleActions
            Get
                Dim Actions As New Entities.Modules.Actions.ModuleActionCollection
                'edit content
                Actions.Add(GetNextActionID, Localization.GetString("EditContent.Text", LocalResourceFile), Entities.Modules.Actions.ModuleActionType.AddContent, "", "", EditUrl(), False, Security.SecurityAccessLevel.Edit, True, False)
                Return Actions
            End Get
        End Property

#End Region

    End Class

End Namespace
